package net.mingsoft.cms.biz;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import net.mingsoft.cms.dao.IContentShuffleDao;
import net.mingsoft.cms.entity.ContentShuffleTitleEntity;

/**
 * 文章混淆元素
 */
@Service
public class ContentShuffleService {
	public static final String imgKey = "CMSH_IMG";
	public static final String hits = "CMSH_HITS";
	public static final ConcurrentHashMap<String,ContentShuffleTitleEntity> Titles = new ConcurrentHashMap<String,ContentShuffleTitleEntity>();
	public static final Vector<ContentShuffleTitleEntity> TitlesList = new Vector<ContentShuffleTitleEntity>();
	public static final Vector<String> Heads = new Vector<String>();
	public static final Vector<String> Bodys = new Vector<String>();
	public static final Vector<String> Foots = new Vector<String>();
    @Autowired
    RedisCacheService redis;
    
    @Autowired
    IContentShuffleDao shuffleDao;
    
    @Autowired
    IContentShuffleBiz shuffleBiz;
    
    public ContentShuffleTitleEntity getTitle(String id) {
    	fillTitles();
    	ContentShuffleTitleEntity title = Titles.get(id);
    	if(title==null) {
    		title = getRandomTitle();
    	}
    	return title;
    }
    private synchronized void fillTitles() {
    	if(Titles.isEmpty()) {//填充数据
    		TitlesList.clear();
    		ContentShuffleTitleEntity en = new ContentShuffleTitleEntity();
    		List<ContentShuffleTitleEntity> titles = shuffleBiz.query(en);
    		titles.forEach(t->{
    			Titles.put(t.getId(), t);
    			TitlesList.add(t);
    			});
    	}
    }
    public ContentShuffleTitleEntity getRandomTitle() {
    	fillTitles();
    	int count = TitlesList.size();
    	if(count==0) {
    		return new ContentShuffleTitleEntity();
    	}
    	int rand = Double.valueOf(Math.random()*count).intValue();
    	return TitlesList.get(rand);
    }
    public List<ContentShuffleTitleEntity> getRandomTitles(int count) {
    	fillTitles();
    	List alist = new ArrayList(TitlesList);
    	Collections.shuffle(alist);
    	if(alist.size()<count) {
    		return alist;
    	}
    	List alist2 = new ArrayList();
    	for(int i=0;i<count;i++) {
    		if(alist.isEmpty()) {
    			break;
    		}
    		alist2.add(alist.remove(0));
    	}
    	return alist2;
    }
    public String getRandomHead(String catId) {
		if(Heads.isEmpty()) {
			Heads.addAll(shuffleDao.queryHeads(catId));
    	}
		
		int count = Heads.size();
		if(count==0) {
			return "";
		}
    	int rand = Double.valueOf(Math.random()*count).intValue();
    	return Heads.get(rand);
    }
    
    public List<String> getRandomBodys(int counts,String catId){
    	if(Bodys.isEmpty()) {
    		Bodys.addAll(shuffleDao.queryBodys(catId));
    	}
    	if(Bodys.size()<counts) {
    		return Bodys;
    	}
    	List<String> bos = new ArrayList<String>();
    	int count = Bodys.size();
    	for(int i=0;i<counts;i++) {
    		int rand = Double.valueOf(Math.random()*count).intValue();
    		bos.add(Bodys.get(rand));
    	}
    	return bos;
    }
    
    public String getRandomFoot(String catId) {
    	if(Foots.isEmpty()) {
    		Foots.addAll(shuffleDao.queryFoots(catId));
    	}
    	int count = Foots.size();
    	if(count==0) {
			return "";
		}
    	int rand = Double.valueOf(Math.random()*count).intValue();
    	return Foots.get(rand);
    }
    @Value("${ms.diy.artimg-dir}")
	private String imgDir;
    @Value("${ms.diy.cache-prefix:1}")
    private String cachePrefix;
    public List getRandomImgs(int count){
    	if(!redis.hasKey(cachePrefix+imgKey)) {
    		File dir = new File(imgDir);
    		List<String> names = new ArrayList<String>();
    		if(dir.exists()&&dir.isDirectory()) {
    			for(File sub:dir.listFiles()) {
    				if(sub.isFile()) {
    					names.add(sub.getName());
    				}
    			}
    		}
    		if(names.isEmpty()) {
    			return new ArrayList();
    		}
    		redis.sAdd(cachePrefix+imgKey, 60, names.toArray());
    	}
    	return redis.srandmember(cachePrefix+imgKey, count);
    }
    
    
	public Long getHits() {
		if(redis.hasKey(cachePrefix+hits)) {
			return redis.incr(cachePrefix+hits, 1);
		}else {
			return redis.incr(cachePrefix+hits, 30001);
		}
	}
}
